﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Hutech.Authentication.Library.Data;


namespace Hutech.Authentication.Library.Business
{
    public class UserPermission
    {

        #region Constructors

        public UserPermission()
        { }

        public UserPermission(Guid userId,Guid actionGuid)
        {
            GetUserPermission(userId,actionGuid);
        }

        public UserPermission(
            Guid userId,
            Guid actionGuid,
            Guid userPermissionGuid)
        {
            GetUserPermission(
                userId,
                actionGuid,
                userPermissionGuid);
        }

        #endregion

        #region Private Properties

        private Guid userId = Guid.Empty;
        private Guid actionGuid = Guid.Empty;
        private Guid userPermissionGuid = Guid.Empty;
        private DateTime editDate;
        private Guid editUserID = Guid.Empty;

        #endregion

        #region Public Properties

        public Guid UserId
        {
            get { return userId; }
            set { userId = value; }
        }
        public Guid ActionGuid
        {
            get { return actionGuid; }
            set { actionGuid = value; }
        }
        public Guid UserPermissionGuid
        {
            get { return userPermissionGuid; }
            set { userPermissionGuid = value; }
        }
        public DateTime EditDate
        {
            get { return editDate; }
            set { editDate = value; }
        }
        public Guid EditUserID
        {
            get { return editUserID; }
            set { editUserID = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of UserPermission.
        /// </summary>
        /// <param name="userId"> userId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="userPermissionGuid"> userPermissionGuid </param>
        private void GetUserPermission(
            Guid userId,
            Guid actionGuid,
            Guid userPermissionGuid)
        {
            using (IDataReader reader = DBUserPermission.GetOne(
                userId,
                actionGuid,
                userPermissionGuid))
            {
                PopulateFromReader(reader);
            }

        }

        private void GetUserPermission(Guid userId, Guid actionGuid)
        {
            using (IDataReader reader = DBUserPermission.GetOneBy2Para(userId, actionGuid))
            {
                PopulateFromReader(reader);
            }

        }

        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                this.userId = new Guid(reader["UserId"].ToString());
                this.actionGuid = new Guid(reader["ActionGuid"].ToString());
                this.userPermissionGuid = new Guid(reader["UserPermissionGuid"].ToString());
                this.editDate = Convert.ToDateTime(reader["EditDate"]);
                this.editUserID = new Guid(reader["EditUserID"].ToString());

            }

        }

        /// <summary>
        /// Persists a new instance of UserPermission. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            //this.userId = Guid.NewGuid();
            //this.actionGuid = Guid.NewGuid();
            this.userPermissionGuid = Guid.NewGuid();

            int rowsAffected = DBUserPermission.Create(
                this.userId,
                this.actionGuid,
                this.userPermissionGuid,
                this.editDate,
                this.editUserID);

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of UserPermission. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBUserPermission.Update(
                this.userId,
                this.actionGuid,
                this.userPermissionGuid,
                this.editDate,
                this.editUserID);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of UserPermission. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            //if (this.userId != Guid.Empty)
            //{
            //    return Update();
            //}
            //else
            //{
            //    return Create();
            //}
            //if (this.actionGuid != Guid.Empty)
            //{
            //    return Update();
            //}
            //else
            //{
            //    return Create();
            //}
            if (this.userPermissionGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of UserPermission. Returns true on success.
        /// </summary>
        /// <param name="userId"> userId </param>
        /// <param name="actionGuid"> actionGuid </param>
        /// <param name="userPermissionGuid"> userPermissionGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(Guid userPermissionGuid)
        {
            return DBUserPermission.Delete(userPermissionGuid);
        }


        /// <summary>
        /// Gets a count of UserPermission. 
        /// </summary>
        public static int GetCount()
        {
            return DBUserPermission.GetCount();
        }

        private static List<UserPermission> LoadListFromReader(IDataReader reader)
        {
            List<UserPermission> userPermissionList = new List<UserPermission>();
            try
            {
                while (reader.Read())
                {
                    UserPermission userPermission = new UserPermission();
                    userPermission.userId = new Guid(reader["UserId"].ToString());
                    userPermission.actionGuid = new Guid(reader["ActionGuid"].ToString());
                    userPermission.userPermissionGuid = new Guid(reader["UserPermissionGuid"].ToString());
                    userPermission.editDate = Convert.ToDateTime(reader["EditDate"]);
                    userPermission.editUserID = new Guid(reader["EditUserID"].ToString());
                    userPermissionList.Add(userPermission);

                }
            }
            finally
            {
                reader.Close();
            }

            return userPermissionList;

        }

        /// <summary>
        /// Gets an IList with all instances of UserPermission.
        /// </summary>
        public static List<UserPermission> GetAll()
        {
            IDataReader reader = DBUserPermission.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of UserPermission.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<UserPermission> GetPage(int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBUserPermission.GetPage(pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

        #region Static methods add by tsandtm
        public static List<UserPermission> GetAllByUserID(Guid UserID)
        {
            IDataReader reader = DBUserPermission.GetAllByUserID(UserID);
            return LoadListFromReader(reader);
        }

        /// <summary>
        /// Check permission of user
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="ActionCommand"></param>
        /// <returns>true or false</returns>
        internal static bool CheckPermission(Guid UserID, string ActionCommand)
        {
            return DBUserPermission.CheckPermission(UserID, ActionCommand);
        }
        #endregion


    }

}